let express = require('express');
let app = express();
//白名单
let whitList = ['http://localhost:8000']
//条件
app.use(function(req,res,next){
    let origin = req.headers.origin;
    if(whitList.includes(origin)){
        //设置那个源可以访问
        res.setHeader('Access-Control-Allow-Origin',origin);
        //允许携带哪个头访问
        res.setHeader('Access-Control-Allow-Headers','name');
        //允许什么方法访问
        res.setHeader('Access-Control-Allow-Methods','PUT');
        //允许携带cookie  与origin*不可一起用
        res.setHeader('Access-Control-Allow-Credentials',true);
        //预检的存活时间
        res.setHeader('Access-Control-Max-Age',6);
        //允许前端获取哪个头
        res.setHeader('Access-Control-Expose-Headers','name');

        if(req.method === 'OPTIONS'){
            res.end();  //OPTIONS请求不做任何处理
        }
    }
    next();  //继续往下走
});
app.put('/getData',function(req,res){
    console.log(req.headers);
    res.setHeader('name','jw');
    res.end("服务端")
})
app.use(express.static(__dirname));   //以当前目录为静态路径
app.listen(4000);

